package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePage;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Transactional
public class ServiceItemServiceImpl extends ServiceImpl<ServiceItemMapper, ServiceItem> implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;


    @Override
    public TablePage<ServiceItem> query(ServiceItemQuery qo) {
        Page<ServiceItem> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<ServiceItem> wrapper = Wrappers.<ServiceItem>query();
        wrapper.like(StringUtils.isNotEmpty(qo.getName()),"name",qo.getName())
                .eq(StringUtils.isNotNull(qo.getCarPackage()),"car_package",qo.getCarPackage())
                .eq(StringUtils.isNotNull(qo.getServiceCatalog()),"service_catalog",qo.getServiceCatalog())
                .eq(StringUtils.isNotNull(qo.getAuditStatus()),"audit_status",qo.getAuditStatus())
                .eq(StringUtils.isNotNull(qo.getSaleStatus()),"sale_status",qo.getSaleStatus());
        serviceItemMapper.selectPage(page,wrapper);
        return new TablePage<ServiceItem>(page);
    }


    @Override
    public void add(ServiceItem serviceItem) throws ParseException {
        /**
         * 1> 新增的时候没有上架状态和审核状态
         * 2> 如果是套餐,则默认审核状态是初始化
         * 3> 如果不是套餐,则默认审核状态是不需要审核
         * 4> 默认上架状态都是没上架
         */
        if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) { // 是套餐
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else { // 不是套餐
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF); // 设置为下架状态
        // 设置创建时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
        serviceItem.setCreateTime(date);
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectById(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        /**
         * 1> 当是上架状态,不给编辑
         * 2> 当是套餐且审核中的时候不给编辑
         * 3> 当是套餐,下架状态,但是是审核通过的时候,编辑要将审核状态设置为初始化
         * 4> 套餐和非套餐之间的转换,区别在于审核状态的改变
         */
        // 不能编辑的情况--------------------------------------------------
        ServiceItem item = this.get(serviceItem.getId());
        if (ServiceItem.SALESTATUS_ON.equals(item.getSaleStatus())) { // 如果是上架状态
            throw new BusinessException("上架状态,不能编辑");
        }
        if (ServiceItem.CARPACKAGE_YES.equals(item.getCarPackage())
                && ServiceItem.AUDITSTATUS_AUDITING.equals(item.getAuditStatus())) { // 当是套餐且审核中的时候
            throw new BusinessException("套餐审核中,不能编辑");
        }
        // 能编辑,但需要逻辑判断的情况--------------------------------------
        if (ServiceItem.CARPACKAGE_YES.equals(item.getCarPackage())
                && ServiceItem.AUDITSTATUS_APPROVED.equals(item.getAuditStatus())) { // 当是套餐且审核通过的时候
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT); // 设置审核状态为初始化
        }
        // 判断传入的套餐情况跟原先的是否一致
        if (serviceItem.getCarPackage().equals(item.getCarPackage())) {
            serviceItem.setAuditStatus(item.getAuditStatus()); // 原来是什么状态就设置成什么状态
        } else if (ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())) { // 原来不是套餐变成套餐,则审核状态为初始化
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        } else { // 从套餐改为非套餐,则无需审核
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF); // 设置为下架状态
        serviceItemMapper.updateById(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteById(dictId);
        }
    }

    @Override
    public void changeSaleStatus(Long id, Integer saleStatus) {
        // 如果需要上架,则要么是不需审核,要么是审核通过,除此之外就不允许上架
        if(ServiceItem.SALESTATUS_OFF.equals(saleStatus)) {
            ServiceItem item = this.get(id);
            if (ServiceItem.CARPACKAGE_YES.equals(item.getCarPackage())
                    && !ServiceItem.AUDITSTATUS_APPROVED.equals(item.getAuditStatus())) { // 是套餐,且不是审核通过
                throw new BusinessException("套餐需要审核通过之后才能上架!");
            }
        }
        UpdateWrapper<ServiceItem> wrapper = Wrappers.<ServiceItem>update();
        wrapper.eq("id",id);
        wrapper.set("sale_status",saleStatus);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    public void changeAuditStatus(Long id, Integer auditStatus) {
        UpdateWrapper<ServiceItem> wrapper = Wrappers.<ServiceItem>update();
        wrapper.eq("id",id);
        wrapper.set("audit_status",auditStatus);
        serviceItemMapper.update(null,wrapper);
    }
}
